// pages/releaseOrder/releaseOrder.js
const api = require('../../utils/http.js')
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		typeId: 0,
		page: 1,
		isEmpty: null,
		navArr1: ['全部', '待付款', '待接单', '待送达', '待确认', '已完成', '待退款'],
		orderList: [],
		total: '',
		pageTotal: 10,
		isTop: false,
		scrollLeft: 0
	},
	toPayment(e) {
		let orderInfo = e.currentTarget.dataset.orderinfo
		let typeId;
		switch (orderInfo.zname) {
			case '待付款':
				typeId = 1;
				break;
			case '待接单':
				typeId = 2;
				break;
			case '待送达':
				typeId = 3;
				break;
			case '待确认':
				typeId = 4;
				break;
			case '已完成':
				typeId = 5;
				break;
			case '退款':
				typeId = 6;
				break;
		}
		if (typeId == 1) {
      // if (orderInfo.type==1004){
      //   //闪送订单加上跑腿费
      //   var total = orderInfo.jine / 1 + orderInfo.paotuifei / 1
      // }else{
        var total = orderInfo.jine / 1
     // }
     
			wx.navigateTo({

        url: '/pages/payment/payment?orderId=' + orderInfo.orderid + '&total=' + total + '&ddId=' + orderInfo.id + '&redid=' + orderInfo.packets_id

			})
		} else if (typeId == 2 || typeId == 3 || typeId == 4 || typeId == 5) {
			wx.navigateTo({
				url: '/pages/takeOrdersDetail/takeOrdersDetail?orderId=' + orderInfo.id
			})
		} else if (typeId == 6) {
			// wx.navigateTo({
			// 	url: '/pages/refund/refund?orderId=' + orderInfo.orderid
			// })
      wx.navigateTo({
        url: '/pages/takeOrdersDetail/takeOrdersDetail?orderId=' + orderInfo.id
      })
		}
    else if (typeId == 5) {
      wx.navigateTo({
        url: '/pages/takeOrdersDetail/takeOrdersDetail?orderId=' + orderInfo.id
      })
    }
	},
	backTop() {
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 300
		})
	},
	changeType(e) {
		let typeId = e.currentTarget.id
		if (typeId > 3) {
			this.setData({
				scrollLeft: 190
			})
		} else {
			this.setData({
				scrollLeft: 0
			})
		}
		console.log(typeId)
		this.data.page = 1
		this.setData({
			typeId
		})
		this.http()
	},
	http() {
		wx.showLoading({
			title: '加载中',
		})
		console.log(this.data.typeId)
		console.log(this.data.page)
		let typeId
		switch (parseInt(this.data.typeId)) {
			// case 0:
			// 	typeId = '全部';
			// 	break;
			case 1:
				typeId = '待付款';
				break;
			case 2:
				typeId = '待接单';
				break;
			case 3:
				typeId = '待送达';
				break;
			case 4:
				typeId = '待确认';
				break;
			case 5:
				typeId = '已完成';
				break;
			case 6:
				typeId = '退款';
				break;
			default:
				typeId = 0;
				break;
		}
		let uid = app.globalData.uid;
		api.fetchApi('procedureList', {
			p: 'R1001003',
			k: this.data.page,
			j: this.data.pageTotal,
			cs: `${uid}|${typeId}`,
		}).then(res => {
			if (res.data.code == 1) {
				let orderList = this.data.orderList
				if (this.data.page == 1) {
					orderList = res.data.results //如果是想下滑动，刷新数据 就让items等于最新数据
				} else {
					orderList = orderList.concat(res.data.results) //否则就把数据拼接
				}
				let total = res.data.results.length
				this.setData({
					orderList,
					total
				})
			} else {
				this.data.page--;
        $Toast({
          content: `${res.data.errorMsg}`
        });
			}
			wx.hideLoading();
		})
	},
  //关闭交易
  closeJiaoyi(e){
   
    var oid=e.currentTarget.dataset.oid;
    wx.showModal({
      title: '提示',
      content: '确定关闭此交易?',
      confirmColor:'#0DCF92',
      success:(res)=> {
        if (res.confirm) {
          api.fetchApi('procedure_t', { p: `R2016008|${oid}`}).then(req=>{
            if(req.data.code==1){
              if (req.data.results[0].codes==1){
                $Toast({
                  content: '删除成功',
                  type: 'success'
                });
                setTimeout(()=>{
                  this.http();
                },800)
              }else{
                $Toast({
                  content: '删除失败',
                  type: 'error'
                });
               
              }
            }else{
              $Toast({
                content: '删除失败',
                type: 'error'
              });
            }
          })
        }
      }
    })
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options)
		let typeId = options.id || 0
		let page = 1
		this.setData({
			typeId,
			page,
		})
		if (typeId == 6) {
			this.setData({
				scrollLeft: 190
			})
		}
		this.http()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		this.data.page = 1
		setTimeout(() => {
			wx.stopPullDownRefresh()
		}, 1500)
		this.http()
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		console.log(this.data.total)
		console.log(this.data.pageTotal, 111)
		this.data.page++;
		if (this.data.total < this.data.pageTotal) {
      $Toast({
        content: '没有更多数据'
      });
		} else {
			wx.showLoading({
				title: '玩命加载中',
			})
			this.http()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	/**
	 * 获取滚动条当前位置
	 */
	onPageScroll: function(e) {
		if (e.scrollTop > 500) {
			this.setData({
				isTop: true
			})
		} else {
			this.setData({
				isTop: false
			})
		}
	},
})
